﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BPoll:Common.CDBase
    {
        /// <summary>
        /// Thêm mới Poll
        /// </summary>
        /// <param name="_OPoll"></param>
        /// <returns></returns>
        public bool Add(OPoll _OPoll)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PollTitle",SqlDbType.NVarChar),
                                        new SqlParameter("@Description",SqlDbType.NVarChar),
                                        new SqlParameter("@CreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@StartDate",SqlDbType.DateTime),
                                        new SqlParameter("@EndDate",SqlDbType.DateTime),
                                        new SqlParameter("@UserCreate",SqlDbType.Int),
                                        new SqlParameter("@Status",SqlDbType.VarChar),
                                        new SqlParameter("@MultiSelect",SqlDbType.Bit),
                                        new SqlParameter("@PollType",SqlDbType.VarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = _OPoll.PollTitle;
            sqlParam[1].Value = _OPoll.Description;
            sqlParam[2].Value = _OPoll.CreateDate;
            sqlParam[3].Value = _OPoll.StartDate;
            sqlParam[4].Value = _OPoll.EndDate;
            sqlParam[5].Value = _OPoll.UserCreate;
            sqlParam[6].Value = _OPoll.Status;
            sqlParam[7].Value = _OPoll.MultiSelect;
            sqlParam[8].Value = _OPoll.PollType;
            sqlParam[9].Value = _OPoll.LangID;
            return RunProcedure("sp_tblPoll_add", sqlParam);
        }
        /// <summary>
        /// Cập nhật Poll
        /// </summary>
        /// <param name="_OPoll"></param>
        /// <returns></returns>
        public bool Update(OPoll _OPoll)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PollID",SqlDbType.Int),
                                        new SqlParameter("@PollTitle",SqlDbType.NVarChar),
                                        new SqlParameter("@Description",SqlDbType.NVarChar),
                                        new SqlParameter("@CreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@StartDate",SqlDbType.DateTime),
                                        new SqlParameter("@EndDate",SqlDbType.DateTime),
                                        new SqlParameter("@Status",SqlDbType.VarChar),
                                        new SqlParameter("@MultiSelect",SqlDbType.Bit),
                                        new SqlParameter("@PollType",SqlDbType.VarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar)};
            sqlParam[0].Value = _OPoll.PollID;
            sqlParam[1].Value = _OPoll.PollTitle;
            sqlParam[2].Value = _OPoll.Description;
            sqlParam[3].Value = _OPoll.CreateDate;
            sqlParam[4].Value = _OPoll.StartDate;
            sqlParam[5].Value = _OPoll.EndDate;
            sqlParam[6].Value = _OPoll.Status;
            sqlParam[7].Value = _OPoll.MultiSelect;
            sqlParam[8].Value = _OPoll.PollType;
            sqlParam[9].Value = _OPoll.LangID;
            return RunProcedure("sp_tblPoll_update", sqlParam);
        }
        public bool Delete(int PollID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PollID", SqlDbType.Int) };
            sqlParam[0].Value = PollID;
            return RunProcedure("sp_tblPoll_delete", sqlParam);
        }
        /// <summary>
        /// Lấy về Poll theo PollID, Nếu PollID=0 lấy về tất cả.
        /// </summary>
        /// <param name="PollID"></param>
        /// <returns></returns>
        public IList<OPoll> Get(int PollID,int PageIndex,int PageSize)
        { 
            SqlParameter[] sqlParam = { new SqlParameter("@PollID",SqlDbType.Int),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = PollID;
            sqlParam[1].Value = PageIndex;
            sqlParam[2].Value = PageSize;
            return Common.Common.ConvertTo<OPoll>(RunProcedureGet("sp_tblPoll_get", sqlParam));
        }
        /// <summary>
        /// Lấy về Poll
        /// </summary>
        /// <param name="PollID"></param>
        /// <param name="PollTitle"></param>
        /// <param name="Status"></param>
        /// <param name="PollType"></param>
        /// <param name="LangID"></param>
        /// <param name="OrderBy"></param>
        /// <param name="Order"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OPoll> Get(string PollTitle, string Status, string PollType, string LangID, string OrderBy, string Order, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PollTitle",SqlDbType.NVarChar),                                        
                                        new SqlParameter("@Status",SqlDbType.VarChar),
                                        new SqlParameter("@PollType",SqlDbType.VarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};            
            sqlParam[0].Value = PollTitle;            
            sqlParam[1].Value = Status;
            sqlParam[2].Value = PollType;
            sqlParam[3].Value = LangID;
            sqlParam[4].Value = OrderBy;
            sqlParam[5].Value = Order;
            sqlParam[6].Value = PageIndex;
            sqlParam[7].Value = PageSize;
            return Common.Common.ConvertTo<OPoll>(RunProcedureGet("sp_tblPoll_get", sqlParam));
        }
        /// <summary>
        /// Lấy về Poll
        /// </summary>
        /// <param name="PollID"></param>
        /// <param name="PollTitle"></param>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <param name="Status"></param>
        /// <param name="PollType"></param>
        /// <param name="LangID"></param>
        /// <param name="OrderBy"></param>
        /// <param name="Order"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OPoll> Get(int PollID,string PollTitle,DateTime StartDate,DateTime EndDate,string Status,string PollType,string LangID,string OrderBy,string Order,int PageIndex,int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@PollID",SqlDbType.Int),
                                        new SqlParameter("@PollTitle",SqlDbType.NVarChar),
                                        new SqlParameter("@StartDate",SqlDbType.DateTime),
                                        new SqlParameter("@EndDate",SqlDbType.DateTime),
                                        new SqlParameter("@Status",SqlDbType.VarChar),
                                        new SqlParameter("@PollType",SqlDbType.VarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = PollID;
            sqlParam[1].Value = PollTitle;
            sqlParam[2].Value = StartDate;
            sqlParam[3].Value = EndDate;
            sqlParam[4].Value = Status;
            sqlParam[5].Value = PollType;
            sqlParam[6].Value = LangID;
            sqlParam[7].Value = OrderBy;
            sqlParam[8].Value = Order;
            sqlParam[9].Value = PageIndex;
            sqlParam[10].Value = PageSize;
            return Common.Common.ConvertTo<OPoll>(RunProcedureGet("sp_tblPoll_get", sqlParam));
        }
        public IList<OPoll> GetLastRecord()
        {
            SqlParameter[] sqlParam = { };
            return Common.Common.ConvertTo<OPoll>(RunProcedureGet("sp_tblPoll_getlastrecord",sqlParam));
        }
    }
}
